﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using GrainManage.Data.Model.Table;
using System.Linq;
using GrainManage.Data.Model.View;
using GrainManage.Data.Model.Form;

namespace GrainManage.Data.Service
{
    public class AccountService : BaseService
    {
        public AccountService(GrainManage.Data.Access.AccessBase proc) : base(proc) { }

        public AccountView GetAccountView(string userName)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Account_GetBasicByName, userName);
            return ConvertToModel<AccountView>(GetFirstRow(ds));
        }

        public string GetConnectionString(int permissionID)
        {
            string connectionString = Proc.ExecuteScalar(ProcConst.Permission_GetConnectionStringByID, permissionID) as string;
            return connectionString;
        }

        public int LogLogin(string userName, string ip)
        {
            int affected = Proc.Execute(ProcConst.Activity_LogLogin, userName, ip);
            return affected;
        }

        public string ResetGuid(string userName)
        {
            string result = Proc.ExecuteScalar(ProcConst.Account_ResetGuid, userName) as string;
            return result;
        }

        public bool Validate(string userName, string pwd)
        {
            var isExist = Proc.ExecuteScalar(ProcConst.Account_Validate, userName, pwd);
            return Convert.ToBoolean(isExist);
        }

        public int Register(RegisterForm account)
        {
            int affected = Proc.Execute(ProcConst.Account_Register, account.UserName, account.Password, account.RealName, account.CellPhone, account.Email, account.CheckLoginIP, account.Remarks);
            return affected;
        }

        public string ResetPassword(string userName, string email)
        {
            var newPwd = Proc.ExecuteScalar(ProcConst.Account_ResetPassword, userName, email) as string;
            return newPwd;
        }

        public int ChangePassword(string userName, string oldPwd, string newPwd)
        {
            int affected = Proc.Execute(ProcConst.Account_ChangePassword, userName, oldPwd, newPwd);
            return affected;
        }

        public int ManageAccount(ManageForm manage)
        {
            int affected = Proc.Execute(ProcConst.Account_ManageAccount, manage.AccountName, manage.IsApproved, manage.PermissionID);
            return affected;
        }

        public bool ChangeOnLineState(string userName, bool isOnLine)
        {
            int affected = Proc.Execute(ProcConst.Account_ChangeOnLineState, userName, isOnLine);
            return affected > 0 ? true : false;
        }

        public bool ResetActiveTime(string userName)
        {
            int result = Proc.Execute(ProcConst.Account_ResetActiveTime, userName);
            return result > 0 ? true : false;
        }

        public string ResetEncryptKey(string userName)
        {
            string result = Proc.ExecuteScalar(ProcConst.Account_ResetEncryptKey, userName) as string;
            return result;
        }

        public string GetRandomString(string seed, int length)
        {
            string pwd = Proc.ExecuteScalar(ProcConst.Account_GetRandomString, seed, length) as string;
            return pwd;
        }
    }
}
